package com.auicyh.tylb.utils;

import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.Point;
import android.graphics.Rect;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.View;

import java.lang.reflect.Field;

/**
 * 作者：  Ocean 2018/8/22 11:19
 * 信息：  Oil com.auicyh.tylb.utils
 * 功能：  屏幕信息工具类
 * 邮箱：   1126067113@qq.com
 * 修改人：
 * 公司: 北京天元陆兵汽车科技有限公司
 * @author Ocean
 */
public class ScreenUtil {

    /**
     * 获取屏幕可操作区域的宽
     * @param activity activity
     * @return 屏幕可操作区域宽度
     */
    public static int getScreenWidth(Activity activity) {
        DisplayMetrics displayMetrics = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        return displayMetrics.widthPixels;
    }

    /**
     * 获取屏幕可操作区域的高
     * @param activity activity
     * @return 屏幕可操作区域高度
     */
    public static int getScreenHeight(Activity activity) {
        DisplayMetrics displayMetrics = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        return displayMetrics.heightPixels;
    }

    /**
     * 获取屏幕可操作区域的宽和高
     * @param activity activity
     * @return 宽度和高度封装到Point对象中
     */
    public static Point getScreenSize(Activity activity) {
        return new Point(getScreenWidth(activity), getScreenHeight(activity));
    }


    /**
     * 获取屏幕真实宽度(包含不可操作区域)
     * 其实和 getScreenWidth()获取的值相同，相对getRealHeight()增加
     * @param activity activity
     * @return 屏幕真实宽度
     */
    public static int getRealWidth(Activity activity) {
        int widthPixels = 0;
        Display display = activity.getWindowManager().getDefaultDisplay();
        try {
            Point realSize = new Point();
            Display.class.getMethod("getRealSize", Point.class).invoke(display, realSize);
            widthPixels = realSize.x;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return widthPixels;
    }

    /**
     * 获取屏幕真实高度(包含不可操作区域)
     * @param activity activity
     * @return 屏幕真实高度
     */
    public static int getRealHeight(Activity activity) {
        int heightPixels = 0;
        Display display = activity.getWindowManager().getDefaultDisplay();
        Point realSize = new Point();
        try {
            Display.class.getMethod("getRealSize", Point.class).invoke(display, realSize);
            heightPixels = realSize.y;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return heightPixels;
    }

    /**
     * 获取屏幕真实宽度和高度
     * @param activity activity(包含不可操作区域)
     * @return 宽度和高度封装到Point对象中
     */
    public static Point getRealSize(Activity activity) {
        return new Point(getRealWidth(activity), getRealHeight(activity));
    }

    /**
     * 获取屏幕宽度ppi
     * @param activity activity
     * @return 屏幕宽度ppi
     */
    public static float getWidthPpi(Activity activity) {
        DisplayMetrics displayMetrics = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        return displayMetrics.xdpi;
    }

    /**
     * 获取屏幕高度ppi  每英寸所拥有的像素
     * @param activity activity
     * @return 屏幕高度ppi
     */
    public static float getHeightPpi(Activity activity) {
        DisplayMetrics displayMetrics = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        return displayMetrics.ydpi;
    }

    /**
     * 获取屏幕ppi 每英寸所拥有的像素
     * @param activity activity
     * @return 屏幕高度ppi
     */
    public static float getScreenPpi(Activity activity) {
        return (getWidthPpi(activity) + getHeightPpi(activity)) / 2;
    }

    /**
     * 获取屏幕宽度物理尺寸
     * @param activity activity
     * @return 宽度物理尺寸
     */
    public static float getWidthInch(Activity activity) {
        // 获取屏幕真实宽度
        int realWidth = getRealWidth(activity);
        DisplayMetrics displayMetrics = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        // 获取屏幕宽度
        return (float) realWidth / getWidthPpi(activity);
    }

    /**
     * 获取屏幕高度物理尺寸
     * @param activity activity
     * @return 高度物理尺寸
     */
    public static float getHeightInch(Activity activity) {
        int realHeight = getRealHeight(activity);
        return (float) realHeight / getHeightPpi(activity);
    }

    /**
     * 获取屏幕物理尺寸(对角线)
     * @param activity activity
     * @return 屏幕物理尺寸
     */
    public static float getScreenInch(Activity activity) {
        return (float) Math.sqrt(Math.pow(getWidthInch(activity), 2) + Math.pow(getHeightInch(activity), 2));
    }

    /**
     * 获取屏幕密度
     * @return 密度
     */
    public static float getScreenDensity(Activity activity) {
        DisplayMetrics displayMetrics = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        return displayMetrics.density;
    }

    /**
     * 获取每英寸的点数(打印分辨率,区别PPI)
     * @return 获取每英寸的点数(
     */
    public static float getDensityDpi(Activity activity) {
        DisplayMetrics displayMetrics = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        return displayMetrics.densityDpi;
    }

    /**
     * 获取屏幕可操作区域宽度dp数目
     * @param activity activity
     * @return dp
     */
    public static float getScreenWidthDp(Activity activity) {
        return getScreenWidth(activity) / getScreenDensity(activity);
    }

    /**
     * 获取屏幕高度可操作区域dp数目
     * @param activity activity
     * @return dp
     */
    public static float getScreenHeightDp(Activity activity) {
        return getScreenHeight(activity) / getScreenDensity(activity);
    }

    /**
     * 获取屏幕真实宽度dp数目
     * @param activity activity
     * @return 宽度dp数目
     */
    public static float getRealWidthDp(Activity activity) {
        return getRealWidth(activity) / getScreenDensity(activity);
    }

    /**
     * 获取屏幕真实高度dp数目
     * @param activity activity
     * @return 高度dp数目
     */
    public static float getRealHeightDp(Activity activity) {
        return getRealHeight(activity) / getScreenDensity(activity);
    }

    /**
     * 判断设备是否为平板
     * @param activity activity
     * @return true 平板;
     * false 手机;
     */
    public static boolean isTablet(Activity activity) {
        return isMoreThan6Inch(activity) && isScreenSizeLarge(activity);
    }


    /**
     * 判断屏幕是否大于6英寸
     * @param activity activity
     * @return 是否大于6.0寸
     */
    public static boolean isMoreThan6Inch(Activity activity) {
        return getScreenInch(activity) >= 6.0;
    }

    /**
     * 判断设备是否为大尺寸屏幕
     * @param context context
     * @return 是否是大尺寸
     */
    public static boolean isScreenSizeLarge(Context context) {
        return (context.getResources().getConfiguration().screenLayout
                & Configuration.SCREENLAYOUT_SIZE_MASK)
                >= Configuration.SCREENLAYOUT_SIZE_LARGE;
    }


    /**
     * 通过dp获取像数
     * https://developer.android.google.cn/guide/practices/screens_support
     * 公式：px = dp * (dpi / 160)
     * @param context 上下文
     * @param dp      dp
     * @return px
     */
    public static int getPx(Activity context, float dp) {
        float dpi = getDensityDpi(context);
        return (int) (dp * dpi / 160);
    }

    /**
     * 通过px获取dp
     * 公式：px = dp * (dpi / 160)
     * @param activity 上下文
     * @param px       px
     * @return dp
     */
    public static int getDp(Activity activity, float px) {
        float dpi = getDensityDpi(activity);
        return (int) (px * 160 / dpi);
    }

    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素) 转换
     */
    public static int dip2px(Context context, float dpValue) {
        // //当前屏幕密度因子
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }


    /**
     * 根据手机的分辨率从 px(像素) 的单位 转成为 dp
     */
    public static int px2dip(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }


    /**
     * 获取通知栏的高度
     * @return 通知栏
     */
    public static int getStatusBarHeight(Activity activity) {
        Class<?> c;
        Object obj;
        Field field;
        int x, statusBarHeight = 0;
        try {
            c = Class.forName("com.android.internal.R$dimen");
            obj = c.newInstance();
            field = c.getField("status_bar_height");
            x = Integer.parseInt(field.get(obj).toString());
            statusBarHeight = activity.getResources().getDimensionPixelSize(x);
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        return statusBarHeight;
    }

    /**
     * 获取当前屏幕截图，包含状态栏
     * @return bitmap
     */
    public static Bitmap snapShotWithStatusBar(Activity activity) {
        View view = activity.getWindow().getDecorView();
        view.setDrawingCacheEnabled(true);
        view.buildDrawingCache();
        Bitmap bmp = view.getDrawingCache();
        int width = getScreenWidth(activity);
        int height = getScreenHeight(activity);
        Bitmap bp = Bitmap.createBitmap(bmp, 0, 0, width, height);
        view.destroyDrawingCache();
        return bp;
    }

    /**
     * 获取当前屏幕截图，不包含状态栏
     * @return bitmap
     */
    public static Bitmap snapShotWithoutStatusBar(Activity activity) {
        View view = activity.getWindow().getDecorView();
        view.setDrawingCacheEnabled(true);
        view.buildDrawingCache();
        Bitmap bmp = view.getDrawingCache();
        Rect frame = new Rect();
        activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
        int statusBarHeight = frame.top;
        int width = getScreenWidth(activity);
        int height = getScreenHeight(activity);
        Bitmap bp = Bitmap.createBitmap(bmp, 0, statusBarHeight, width, height - statusBarHeight);
        view.destroyDrawingCache();
        return bp;
    }

}
